package 设计模式;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DoubleCheckSingleton {
    
    private static DoubleCheckSingleton instance;

    private DoubleCheckSingleton(){}

    public static DoubleCheckSingleton getInstance(){
        if(instance == null){ // 第一次检查
            System.out.println(Thread.currentThread().getName() + "...first check...");
            synchronized(DoubleCheckSingleton.class){
                if(instance == null){ // 第二次检查
                    System.out.println(Thread.currentThread().getName() + "...second check...");
                    instance = new DoubleCheckSingleton();
                }
            }
        }
        return instance;
    }
    public static void main(String[] args) {
        ExecutorService threadPool = Executors.newFixedThreadPool(10);
        for(int i = 0; i < 10; i++){
            threadPool.submit(() -> {
                System.out.println(Thread.currentThread().getName() + "...running...");
                DoubleCheckSingleton.getInstance();
            });
        }
        threadPool.shutdown();
    }
}
